﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using NetDimension.Weibo;

namespace WeiboAutomation
{
    public partial class frmLogin : Form
    {
        private NetDimension.Weibo.OAuth oauth;

        public string Account = "";
        public string Password = "";
        static string AccessToken = "";

        Logger logger = new Logger();
        XML xml = new XML();

        public frmLogin(NetDimension.Weibo.OAuth oauth)
        {
            InitializeComponent();
            this.oauth = oauth;
            logger.log("初始化-----------------------------------");
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            Account = xml.read("Account");
            Password = xml.read("Password");
            AccessToken = xml.read("AccessToken");

            if (!string.IsNullOrEmpty(Password))
            {
                txtAccount.Text = Account;
                txtPassword.Text = Password;
                chkRemember.Checked = true;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAccount.Text))
            {
                MessageBox.Show(this, "请输入账号。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show(this, "请输入密码。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            pictureBox1.Visible = true;

            Thread thLogin = new Thread(() =>
            {
                try
                {
                    //暂时不用已存在的AccessToken, Oauth传给frmWeibo的时候不会改变
                    TokenResult tr = TokenResult.Other;
                    //if (!string.IsNullOrEmpty(AccessToken))
                    //{
                    //    logger.log("配置文件中有AccessToken: " + AccessToken);
                    //    oauth = new OAuth(oauth.AppKey, oauth.AppSecret, AccessToken, "");//或者再写个重载函数，包括callbackurl的
                    //    logger.log("验证Token有效性...");
                    //    tr = oauth.VerifierAccessToken();
                    //    if (tr == TokenResult.Success)
                    //    {
                    //        logger.log("AccessToken有效。");
                    //        UILoginComplete(true, "AccessToken有效。");
                    //    }
                    //    else
                    //    {
                    //        logger.log("AccessToken无效，重新授权！");
                    //    }
                    //}

                    //AccessToken验证无效或者没有
                    if (string.IsNullOrEmpty(AccessToken) || tr != TokenResult.Success)
                    {
                        logger.log("开始授权！");
                        oauth.ClientLogin(Account, Password);
                        if (!string.IsNullOrEmpty(oauth.AccessToken))
                        {
                            logger.log("获取授权成功！");
                            UILoginComplete(true, "获取授权成功！");
                        }
                        else
                        {
                            logger.log("获取授权失败！");
                            UILoginComplete(false, "获取授权失败");
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.log("获取授权时异常：" + ex.Message);
                    UILoginComplete(false, ex.Message);
                }
            });
            thLogin.Start();
        }

        private void UILoginComplete(bool success, string msg)
        {
            this.UIInvoke(() =>
            {
                pictureBox1.Visible = false;
                if (success)
                {
                    if (chkRemember.Checked)
                    {
                        xml.write("AccessToken", oauth.AccessToken);
                    }
                    //强制保存，为了frmWeibo
                    Account = txtAccount.Text;
                    Password = txtPassword.Text;
                    xml.write("Account", Account);
                    xml.write("Password", Password);

                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show(this, "没有登录成功，请确保账号密码正确。\r\n错误提示：" + msg, "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            });
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
